﻿using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime.Creator
{
    public class CreateMethod
    {
        public string Name { get; set; }
        public string Descr { get; set; }
        public string[] StepNames { get; set; }
        public string[] StepOptions { get; set; }

       
    }

    public abstract class ElementCreator
    {
        public abstract string GetCmdName();
        public abstract CreateMethod[] GetMethods();
        /// <summary>
        /// 获取指定的方法
        /// </summary>
        /// <param name="name">方法名</param>
        /// <returns></returns>
        public virtual CreateMethod GetMethod(string name)
        {
            var methods = GetMethods();
            for (var i = 0; i < methods.Length; i++)
            {
                if (methods[i].Name.ToUpper() == name.ToUpper())
                    return methods[i];
            }
            return null;
        }
        public virtual CreateMethod GetDefaultMethod()
        {
            return GetMethods()[0];
        }
        public virtual string GetNextStepName(CreateMethod method,string stepName)
        {
            var idx = Array.IndexOf(method.StepNames, stepName);
            return method.StepNames[idx + 1];
        }
        public virtual string GetStepTip(CreateMethod method,string stepName)
        {
            var idx = Array.IndexOf(method.StepNames, stepName);
            var cmdName = GetCmdName();
            return cmdName + " " + method.StepOptions[idx];
        }
        //public virtual void ExecuteStep(string methodName, string stepName = null)
        //{
        //    var method = GetMethod(methodName);
        //    if (stepName == null) stepName = method.StepNames[0];

        //    ExecuteStep(method, stepName,null);
        //}

        public virtual void BeginAction()
        {
            DocumentManager.CurrentRecorder.BeginAction(GetCmdName());
        }
        /// <summary>
        /// 执行默认的创建方法的第一个步骤
        /// </summary>
        /// <param name="stepArg">第一个步骤的选择参数</param>
        public virtual void Execute(string stepArg = null)
        {
            BeginAction();
            var method = GetDefaultMethod();
            ExecuteStep(method, method.StepNames[0], stepArg);
        }

   
        protected abstract void ExecuteStep(CreateMethod method, string stepName,string stepArg);

        protected virtual void ExecuteEnd(bool success)
        {
            VPortInputer.SetAuxLineDraw(null);
            VPortInputer.EndInputPoint(true);
            CmdInputer.EndInput(true);
            EndAction(success);
        }
        protected virtual void EndAction(bool success)
        {
            if (success)
                DocumentManager.CurrentRecorder.EndAction();
            else
                DocumentManager.CurrentRecorder.CancleAction();
        }
    }
}
